package org.lanyu.springainovel.novel.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lanyu.springainovel.common.vo.RestVO;
import org.lanyu.springainovel.novel.entity.BookSetting;
import org.lanyu.springainovel.novel.service.BookSettingService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小说其他设定控制器
 */
@Slf4j
@RestController
@RequestMapping("/novel/setting")
@RequiredArgsConstructor
@Tag(name = "小说其他设定管理", description = "小说其他设定相关接口")
public class BookSettingController {

    private final BookSettingService settingService;

    @Operation(summary = "获取书籍的所有设定", description = "根据书籍ID获取该书籍的所有设定信息")
    @GetMapping("/book/{bookId}")
    public RestVO<List<BookSetting>> getSettingsByBookId(
            @Parameter(description = "书籍ID", required = true) @PathVariable Long bookId) {
        try {
            List<BookSetting> settings = settingService.getSettingsByBookId(bookId);
            return RestVO.success(settings);
        } catch (Exception e) {
            log.error("获取设定列表失败", e);
            return RestVO.fail("获取设定列表失败: " + e.getMessage());
        }
    }

    @Operation(summary = "添加设定", description = "为指定书籍添加新的设定信息")
    @PostMapping("/book/{bookId}")
    public RestVO<List<BookSetting>> addSetting(
            @Parameter(description = "书籍ID", required = true) @PathVariable Long bookId,
            @Parameter(description = "设定信息", required = true) @RequestBody BookSetting setting) {
        try {
            List<BookSetting> settings = settingService.addSetting(bookId, setting);
            return RestVO.success(settings);
        } catch (Exception e) {
            log.error("添加设定失败", e);
            return RestVO.fail("添加设定失败: " + e.getMessage());
        }
    }

    @Operation(summary = "更新设定", description = "更新指定ID的设定信息")
    @PutMapping("/{id}")
    public RestVO<BookSetting> updateSetting(
            @Parameter(description = "设定ID", required = true) @PathVariable Long id,
            @Parameter(description = "设定信息", required = true) @RequestBody BookSetting setting) {
        try {
            BookSetting updatedSetting = settingService.updateSetting(id, setting);
            return RestVO.success(updatedSetting);
        } catch (Exception e) {
            log.error("更新设定失败", e);
            return RestVO.fail("更新设定失败: " + e.getMessage());
        }
    }

    @Operation(summary = "删除设定", description = "删除指定ID的设定")
    @DeleteMapping("/{id}")
    public RestVO<String> deleteSetting(
            @Parameter(description = "设定ID", required = true) @PathVariable Long id) {
        try {
            boolean success = settingService.deleteSetting(id);
            if (success) {
                return RestVO.success("删除成功");
            } else {
                return RestVO.fail("删除失败");
            }
        } catch (Exception e) {
            log.error("删除设定失败", e);
            return RestVO.fail("删除设定失败: " + e.getMessage());
        }
    }

    @Operation(summary = "获取设定详情", description = "根据ID获取设定详情")
    @GetMapping("/{id}")
    public RestVO<BookSetting> getSettingById(
            @Parameter(description = "设定ID", required = true) @PathVariable Long id) {
        try {
            BookSetting setting = settingService.getSettingById(id);
            if (setting != null) {
                return RestVO.success(setting);
            } else {
                return RestVO.fail("未找到该设定");
            }
        } catch (Exception e) {
            log.error("获取设定详情失败", e);
            return RestVO.fail("获取设定详情失败: " + e.getMessage());
        }
    }

    @Operation(summary = "获取设定列表（分页）", description = "分页获取设定列表")
    @GetMapping("/list")
    public RestVO<Object> getSettings(
            @Parameter(description = "页码", required = true) @RequestParam Integer page,
            @Parameter(description = "每页大小", required = true) @RequestParam Integer size,
            @Parameter(description = "标题") @RequestParam(required = false) String title,
            @Parameter(description = "书籍ID") @RequestParam(required = false) Long bookId) {
        try {
            // 这里可以实现分页查询逻辑
            // 由于前端服务已经实现了分页查询，这里可以暂时返回所有数据
            // 或者实现一个简单的分页查询
            List<BookSetting> settings;
            if (bookId != null) {
                settings = settingService.getSettingsByBookId(bookId);
            } else {
                // 如果没有提供bookId，返回空列表或抛出异常
                return RestVO.fail("必须提供书籍ID");
            }
            
            // 简单的分页处理
            int total = settings.size();
            int start = (page - 1) * size;
            int end = Math.min(start + size, total);
            List<BookSetting> pageData = settings.subList(start, end);
            
            // 构建分页结果
            java.util.Map<String, Object> result = new java.util.HashMap<>();
            result.put("records", pageData);
            result.put("total", total);
            result.put("size", size);
            result.put("current", page);
            result.put("pages", (total + size - 1) / size);
            
            return RestVO.success(result);
        } catch (Exception e) {
            log.error("获取设定列表失败", e);
            return RestVO.fail("获取设定列表失败: " + e.getMessage());
        }
    }
}